<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>消息管理</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
</head>
<body>
    <div class="maincontainer">
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <form id="queryForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">消息标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" class="layui-input" placeholder="消息标题">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">发布时间</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="selectTime" placeholder=" - ">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select name="readStatus" lay-filter="readStatus">
                                    <option value=""></option>
                                    <option value="1">已读</option>
                                    <option value="0">未读</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn"  lay-submit lay-filter="searchBth">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary reReset" >重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-card-body">
                <table class="layui-hide" id="selectTable" lay-filter="selectTable"></table>
            </div>
        </div>

    </div>
</body>

<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container toolbarDemo">
		<button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
		<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="export">导出</button>
	</div>
</script>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
    
    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'table', 'form', 'laydate'], function() {
        var http = layui.http,
            table = layui.table,
            form = layui.form,
            laydate = layui.laydate;
			
			$('.reReset').click();
			
        http.checking(function() {
            //时间
            laydate.render({
                elem: '#selectTime'
                ,theme: '#fbaf7e'
                ,range: "-"
            });
            
            var param = {};

            // 角色列表
            var selectTable;
            selectTable = http.table({
                elem: '#selectTable',
                url: 'food/manager/message/listPage',
                method: 'get',
                id: 'selectTable',
                toolbar: '#toolbarDemo',
                cols: [[
                    {title:'序号',type:'numbers',fixed: 'left'}
                    ,{field:'title', title: '消息标题', align : 'center',
                        templet: function(d){
                            var html = ''
                            html += '<a class="buttoncolor" href="messageInfo.html?id='+d.id+'&&messageType='+d.messageType+'&&readStatus='+d.readStatus+'">'+d.title+'</a>'
                            return html
                        }
                    }
                    ,{field:'createTime', title: '发布时间', align : 'center'}
                    ,{field:'readStatus', title: '状态', align : 'center',
                        templet: function (d) {
                            if(d.messageType==1){
                                return d.readStatus==1?'已读':'未读';
                            }else{
                                return'';
                            }
                        }
                    }
                    ,{field:'createTime', title: '阅读量', align : 'center',
                        templet: function (d) {
                            return d.messageType==2?d.readCount:'';
                        }
                    }
                    ,{field:'createTime', title: '标记', align : 'center',
                        templet: function (d) {
                            return d.messageType==1?'系统消息':'发布消息';
                        }
                    }
                    ,{field:'', title: '操作', align : 'center',fixed: 'right',width: 230,
                        templet: function (d) {
                            var html = '';
                            if(d.messageType==2){
                                html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del" >删除</button>'
                            }
                            return html;
                        }
                    }
                ]]
            });

            //查询
            form.on('submit(searchBth)', function (data) {
                let _data = data.field;
                    param = _data;
                var startTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'';
                var endTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'';
                param.startTime = startTime;
                param.endTime = endTime;
                selectTable.reload({
                    page:{curr:1},
                    where: param
                })
            })

            table.on('tool(selectTable)', function(obj){
                var data = obj.data;
                if(obj.event=='del'){
                    // 删除
                    http.myOpen({
                        title:'提示',
                        content:'<p style="text-align:center;margin-top:20px">确认要删除该消息？</p>',
                        area:['300px','150px'],
                        btn:['确定','取消'],
                        btn1:function(index, layero){
                            http.post('food/manager/message/delete',{
                                id: data.id
                            },function(res){
                                if(res.code == 200){
                                    layer.msg('操作成功')
                                    layer.close(index)
                                    table.reload('selectTable');
                                }else{
                                    layer.msg(res.msg)
                                }
                            })
                            
                        }
                    });
                }
            })

            table.on('toolbar(selectTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
                var data = checkStatus.data; //获取选中行数据
                if(obj.event=='add'){ 
                    // 添加
                    location.href = "messageAdd.html"
                }else if(obj.event=='export'){ // 导出
                    param.page =  1;
                    param.limit =  10;
                    param.authentication = localStorage.getItem('xi-head-authentication');
                    let paramsArray = [];
                    Object.keys(param).forEach(key => paramsArray.push(key + '=' + param[key]))  
                    window.open(TUrl+"food/manager/message/export?"+paramsArray.join('&'))
                }
            })
        })
    })

</script>
</html>